var app = new Vue({
  el: '#app',

  data: {
    loading: false,
    total: 0,
    tableDataList: [],
    databases: [],
    searchForm: {
        page: 1,
        limit: 10,
        // 默认数据库
        database: null,
        tableName: null,
        sortColumns: 'table_name',
        sortType: 'ASC'
    },
    form: {
        database: null,
        packageName: null,
        moduleName: null,
        tables: null
    },
    // 默认模糊搜索的数据库
    db: 'quan',
    baseUrl: 'http://localhost:8080'
  },

  mounted() {
    this.$nextTick(function () {

        this.getDataBaseList()
    })
  },

  methods: {

    handleSelection(selection) { // 多选
        this.selectionRows = selection
    },
    handleSizeChange(val) { // 每页数量
        this.searchForm.limit = val
        this.getSelectPage()
    },
    handleCurrentChange(val) { // 页码
        this.searchForm.page = val
        this.getSelectPage()
    },
    handleResetSearch(name) { // 重置搜索表单
        this.$refs[name].resetFields()
        this.searchForm.page = 1
        this.getSelectPage()
    },
    handleSearch() { // 单条件搜索
        this.getSelectPage()
    },

    changeDatabase(value) {
       this.searchForm.database = value
       this.form.database = value
       this.getSelectPage()
    },

    // 获取所有数据库
    getDataBaseList() {
        const _this = this
        axios.get(_this.baseUrl+'/generator/database', {params: {database: _this.db}}).then(function (res) {
            if(res.data.status) {
                _this.databases = res.data.data
                _this.searchForm.database = _this.databases[0].SCHEMA_NAME

                _this.getSelectPage()
            }
        }).catch(function (err) {
            console.log(err);
        });
    },

    // 生成代码
    handleGeneratorCode() {
        const _this = this
        let tables = []
        if(this.selectionRows) {
            this.selectionRows.filter((item, index) => {
                tables.push(item.tableName)
            })
        }
        if(tables.length > 0) {
            this.submitLoading = true
            this.form = Object.assign(this.form, {tables})

            let params = '?database='+this.form.database+'&packageName='+(this.form.packageName?this.form.packageName:'')+'&moduleName='+(this.form.moduleName?this.form.moduleName:'')+'&tables='+this.form.tables.join(',')
            let downloadUrl = _this.baseUrl+'/generator/code/' + params

            location.href = downloadUrl;
        } else {
             this.$notify({ title: '提示', message: '请选择要生成的表', type: 'info' });
         }
    },

    // 加载列表数据
    getSelectPage() {
        const _this = this
        axios.get(_this.baseUrl+'/generator/tables/', {params: this.searchForm}).then(function (res) {
            if(res.data.status) {
                _this.tableDataList = res.data.data.records
                _this.total = res.data.data.total
                _this.loading = false
            }
        }).catch(function (err) {
            console.log(err);
        });
    }

  }

})